package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.exception.ServiceException;
import com.sdleyou.treepro.common.model.convert.TreeDO2TreeVOConverter;
import com.sdleyou.treepro.common.model.dao.TreeDO;
import com.sdleyou.treepro.common.model.dao.TreeImgProportionDO;
import com.sdleyou.treepro.common.model.dto.TreeDTO;
import com.sdleyou.treepro.common.model.enums.ErrorCodeEnum;
import com.sdleyou.treepro.common.model.enums.MapShowEnum;
import com.sdleyou.treepro.common.model.utils.RandomUtil;
import com.sdleyou.treepro.common.model.vo.TreeVO;
import com.sdleyou.treepro.common.service.TreeManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TreeManagerImpl implements TreeManager {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void createTree(TreeDTO treeDTO) {
        TreeDO treeDO = new TreeDO();
        treeDO.setTreeName(treeDTO.getTreeName());
        treeDO.setTreeDesc(treeDTO.getTreeDesc());
        treeDO.setTreeIcon(treeDTO.getTreeIcon());
        treeDO.setTreeImage(treeDTO.getTreeImage());
        treeDO.setTreeId(RandomUtil.randomId());
        treeDO.setIsShow(MapShowEnum.HIDDEN.name());
        treeDO.setGrowValue(treeDTO.getGrowValue());
        treeDO.setPowerValue(treeDTO.getPowerValue());
        treeDO.setTreePrice(treeDTO.getTreePrice());
        Date date = new Date();
        treeDO.setGmtCreate(date.getTime());
        treeDO.setGmtModified(date.getTime());
        mongoTemplate.insert(treeDO);
        return;
    }

    @Override
    public void updateTree(TreeDTO treeDTO, String treeId) {
        Query query = new Query(Criteria.where("tree_id").is(treeId));
        TreeDO treeDO = mongoTemplate.findOne(query, TreeDO.class);
        if (treeDO == null) {
            throw new ServiceException(ErrorCodeEnum.E006.code(), "树木信息不存在");
        }
        Update update = new Update();
        if (!StringUtils.isEmpty(treeDTO.getTreeName())) {
            update.set("tree_name", treeDTO.getTreeName());
        }
        if (!StringUtils.isEmpty(treeDTO.getTreeDesc())) {
            update.set("tree_desc", treeDTO.getTreeDesc());
        }
        if (!StringUtils.isEmpty(treeDTO.getTreeImage())) {
            update.set("tree_image", treeDTO.getTreeImage());
        }
        if (!StringUtils.isEmpty(treeDTO.getIsShow())) {
            update.set("is_show", treeDTO.getIsShow());
        }
        if (!StringUtils.isEmpty(treeDTO.getGrowValue())) {
            update.set("grow_value", treeDTO.getGrowValue());
        }
        if (!StringUtils.isEmpty(treeDTO.getPowerValue())) {
            update.set("power_value", treeDTO.getPowerValue());
        }
        if (!StringUtils.isEmpty(treeDTO.getTreePrice())) {
            update.set("tree_price", treeDTO.getTreePrice());
        }
        if (!StringUtils.isEmpty(treeDTO.getTreeInfoImg())) {
            update.set("tree_info_img", treeDTO.getTreeInfoImg());
        }
        mongoTemplate.updateFirst(query, update, TreeDO.class);
        return;
    }

    @Override
    public List<TreeVO> treeList() {
        Query query = new Query(Criteria.where("is_show").is("SHOW"));
        List<TreeDO> treeDOList = mongoTemplate.find(query, TreeDO.class);
        return treeDOList.stream().map(e -> TreeDO2TreeVOConverter.convert(e)).collect(Collectors.toList());
    }

    @Override
    public TreeVO findOne(String treeType) {
        Query query = new Query(Criteria.where("tree_type").is(treeType));
        TreeDO treeDO = mongoTemplate.findOne(query, TreeDO.class);
        TreeVO treeVO = new TreeVO();
        if(treeDO!=null) {
            BeanUtils.copyProperties(treeDO, treeVO);
        }
        return treeVO;
    }

    @Override
    public TreeVO treeInfoById(String treeId) {
        Query query = new Query(Criteria.where("tree_id").is(treeId));
        TreeDO treeDO = mongoTemplate.findOne(query, TreeDO.class);
        TreeVO treeVO = new TreeVO();
        if(treeDO!=null) {
            BeanUtils.copyProperties(treeDO, treeVO);
        }
        return treeVO;
    }

    /**
     * 创建
     */
    @Override
    public void createTreeImgProportion() {
        Query query = new Query();
        TreeImgProportionDO one = mongoTemplate.findOne(query, TreeImgProportionDO.class);
        if(one == null) {
            TreeImgProportionDO treeImgProportionDO = new TreeImgProportionDO();
            treeImgProportionDO.setProportion(new Double[]{0.06, 0.11, 0.17, 0.27, 0.39});
            mongoTemplate.insert(treeImgProportionDO);
        }
    }

    /**
     * 获取树与图片的比例信息
     */
    @Override
    public Double[] getTreeImgProportion() {
        Query query = new Query();
        TreeImgProportionDO one = mongoTemplate.findOne(query, TreeImgProportionDO.class);
        return one.getProportion();
    }
}
